package kmi;

import java.awt.Color;
import static org.junit.Assert.assertTrue;
import robocode.control.snapshot.IRobotSnapshot;
import robocode.control.snapshot.IBulletSnapshot;
import robocode.control.testing.RobotTestBed;
import robocode.control.events.BattleCompletedEvent;
import robocode.control.events.TurnEndedEvent;

/**
 * Illustrates JUnit testing of Robocode robots. This test simply verifies that GreenMachine fire a
 * bullet with full power(red bullet) when hitting another robot.  GreenMachine only fires a red
 * bullet when struck by another robot.
 * 
 * @author Mitchell Kupfer
 */
public class TestRam extends RobotTestBed {

  // boolean for seeing if GreenMachine shoots at a impacted robot
  boolean RobotCollision = false;
  int hitCount = 0;
  int fireAtHit = 0;
  // robot only fires red bullets when it is hit
  int colorChoice = Color.RED.getRGB();
  int pastId = 0;
  boolean firedPerHit = false;

  /**
   * Match between GreenMachine and RamFire.
   * 
   * @return The comma-delimited list of robots in this match.
   */
  @Override
  public String getRobotNames() {
    return "sample.RamFire,kmi.GreenMachine";
  }

  /**
   * This test runs for 10 rounds.
   * 
   * @return The number of rounds.
   */
  @Override
  public int getNumRounds() {
    return 10;
  }

  /**
   * see if we were hit, if so did we shoot?
   * 
   * @param event Info about the current state of the battle.
   */
  @Override
  public void onTurnEnded(TurnEndedEvent event) {

    // Get the snapshot of the robot GreenMachine
    IRobotSnapshot robot = event.getTurnSnapshot().getRobots()[1];

    IBulletSnapshot[] bullet = event.getTurnSnapshot().getBullets();

    if (robot.getState().isHitRobot()) {
      RobotCollision = true;
      hitCount++;
    }

    for (int i = 0; i < bullet.length; i++) {
      if (bullet[i].getColor() == colorChoice &&
          !(bullet[i].getBulletId() == pastId)) {
          pastId = bullet[i].getBulletId();
          fireAtHit++;
          return;
        }
      }
    
    if (fireAtHit >= hitCount) {
      firedPerHit = true;
    }
  }

  /**
   * After the battle, check to see that we fired a red bullet the same number of times we were hit.
   * 
   * @param event Details about the completed battle.
   */
  @Override
  public void onBattleCompleted(BattleCompletedEvent event) {
    assertTrue("Robot Impacted", RobotCollision);
    assertTrue("If Hit then Shot", firedPerHit);
  }

}
